package com.chensf.studydemo.common

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.HapticFeedbackConstants
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.AppCompatTextView

class AudioRecordButton : AppCompatTextView {
    constructor(context: Context) : this(context, null)
    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)
    constructor(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    ){
        //开启震动
        isHapticFeedbackEnabled = true
        isLongClickable = true
    }
    var listener:AudioRecordListener?=null

    private val location = IntArray(2)
    private val cancelBtnLocation = IntArray(2)
    private lateinit var cancelBtn: ImageView
    private lateinit var tipsTV: TextView
    private lateinit var waveView: View
    private var cancelBtnWidth: Int = 0
    private var cancelBtnHeight: Int = 0
    private var flag = false

    fun setCancelBtn(cancelBtn: ImageView){
        this.cancelBtn = cancelBtn
    }
    fun setTipsTV(tipsTV: TextView){
        this.tipsTV = tipsTV
    }
    fun setWaveView(waveView: View){
        this.waveView = waveView
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.actionMasked) {
            MotionEvent.ACTION_DOWN -> {
                getLocationOnScreen(location)
                cancelBtn.getLocationOnScreen(cancelBtnLocation)
                cancelBtnWidth = cancelBtn.width
                cancelBtnHeight = cancelBtn.height
                flag = false
            }
            MotionEvent.ACTION_MOVE -> {
                calculateLocation(event)
            }
            MotionEvent.ACTION_UP -> {
                isSelected = false
                listener?.complete()
            }
            MotionEvent.ACTION_CANCEL -> {
                isSelected = false
            }
        }
        return super.onTouchEvent(event)
    }

    private fun calculateLocation(event: MotionEvent) {
        val rawX = event.rawX
        val rawY = event.rawY
        val x = location[0]
        val y = location[1]
        val cancelX = cancelBtnLocation[0]
        val cancelY = cancelBtnLocation[1]
        val width = width
        val height = height
        if (rawY < y && !isPointInCancelArea(rawX, rawY)) {
            //显示上滑取消文字
            tipsTV.text = "上划取消"
            waveView.visibility = View.GONE
            cancelBtn.visibility = View.VISIBLE
        }else if(isPointInCancelArea(rawX, rawY)){
            //取消
            tipsTV.text = "取消语音"
            cancelBtn.visibility = View.VISIBLE
            waveView.visibility = View.GONE
        }else{
            //显示松开发送语音的文字
            tipsTV.text = "松开发送语音"
            cancelBtn.visibility = View.INVISIBLE
            waveView.visibility = View.VISIBLE
        }
    }

    override fun performLongClick(): Boolean {
        flag = true
        if (flag) {
            parent.requestDisallowInterceptTouchEvent(true)
            performHapticFeedback(
                HapticFeedbackConstants.LONG_PRESS,
                HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING
            )
            isSelected = true
            listener?.start()
        }
        return super.performLongClick()
    }

    /**
     * 判断点坐标是否在取消的范围内
     */
    private fun isPointInCancelArea(rawX: Float, rawY: Float): Boolean {
        val cancelX = cancelBtnLocation[0]
        val cancelY = cancelBtnLocation[1]
        return (rawX > cancelX && rawX < cancelX + cancelBtnWidth && rawY > cancelY && rawY < cancelY + cancelBtnHeight)
    }
}